package com.isoftstone.merchant.controller.handpayment;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.mdept.MDept;
import com.isoftstone.merchant.entity.payment.MPaymentDetail;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.feeUser.FeeUserService;
import com.isoftstone.merchant.service.handpayment.HandPaymentService;
import com.isoftstone.merchant.service.mproject.MProjectService;
import com.isoftstone.merchant.service.mprojecttype.MProjectTypeService;
import com.isoftstone.merchant.service.payment.IMPaymentDetialService;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.Const;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import com.isoftstone.util.UuidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by as on 2017/12/13.
 * 手动生成费用
 */
@Controller
public class HandPaymentController extends BaseController{
    @Autowired
    HandPaymentService handPaymentService;
    @Autowired
    FeeUserService feeUserService;
    @Autowired
    MProjectTypeService mProjectTypeService;
    @Autowired
    MProjectService mProjectService;
    @Autowired
    PaymentService paymentService;
    @Autowired
    IMPaymentDetialService paymentDetialService;
    /*
    * 去手动生成费用
    * */
    @RequiresPermissions("mms-menu.003.003")
    @RequestMapping("merchant/HandPayment/toHandPaymentList")
    public ModelAndView toHandPaymentPage(){
        ModelAndView md=new ModelAndView();
        md.setViewName("merchant/charge_management/hand_generat_cost");
        return md;
    }
    /*
    * 手动生成费用
    * */
    @RequestMapping("merchant/HandPayment/handPaymentList")
    @ResponseBody
    public Object handPaymentPage(Page page){
        PageData pd = this.getPageData();
        Tidings<List<MFeeUser>> tidings = new Tidings<List<MFeeUser>>();
        String userId=this.getUser().getUserid();
        List<MDept>deptids=null;
        try {
            deptids=feeUserService.getalldeptByUserId(userId);//得到用户对应的deptid
            if(deptids!=null){
                List<String>list=new ArrayList<String>();
                for(MDept mDept1:deptids){
                    list.add(mDept1.getDeptId());
                }
                pd.put("deptIds",list);//看到当前用户部门的缴费用户
            }
            String treeId = pd.getString("treeId");//部门树id串
            String keywords = pd.getString("keywords1");//搜索内容
            String corpId = this.getUser().getCorpId();
            pd.put("corpId", corpId);
            pd.put("status","1");
            List<MFeeUser> feeUserSearch = new ArrayList<MFeeUser>();
            List<MFeeUser> feeUsertList0 = new ArrayList<MFeeUser>();
            List<MFeeUser> feeUserList1=new ArrayList<MFeeUser>();
            List<String >treeIdss=new ArrayList<String>();
            if (StringUtils.isNotBlank(treeId) && StringUtils.isNotBlank(keywords)) {
                keywords = URLDecoder.decode(keywords, "UTF-8");
                pd.put("keywords1", keywords);
                String treeIds[] = treeId.split(",");
                for (String str : treeIds) {
                    treeIdss.add(str);
                }
                pd.put("treeids",treeIdss);
                page.setPd(pd);
                feeUsertList0 = feeUserService.getFeeUserlistPage(page);
                feeUserList1=feeUserService.getFeeUserlistPageDept(feeUsertList0);
            } else if (StringUtils.isNotBlank(treeId) && StringUtils.isBlank(keywords)) {
                String treeIds[] = treeId.split(",");
                for (String str : treeIds) {
                    treeIdss.add(str);
                }
                pd.put("treeids",treeIdss);
                page.setPd(pd);
                feeUsertList0 = feeUserService.getFeeUserlistPage(page);
                feeUserList1=feeUserService.getFeeUserlistPageDept(feeUsertList0);
            } else if (StringUtils.isBlank(treeId) && StringUtils.isNotBlank(keywords)) {
                keywords = URLDecoder.decode(keywords, "UTF-8");
                pd.put("keywords1", keywords);
                page.setPd(pd);
                feeUserSearch = feeUserService.getFeeUserlistPage(page);
                if (feeUserSearch != null) {
                    for (MFeeUser feeUser : feeUserSearch) {
                        feeUsertList0.add(feeUser);
                    }
                }
                feeUserList1=feeUserService.getFeeUserlistPageDept(feeUsertList0);
            } else {
                page.setPd(pd);
                feeUsertList0 = feeUserService.getFeeUserlistPage(page);
                feeUserList1=feeUserService.getFeeUserlistPageDept(feeUsertList0);
            }
            if (feeUserList1 != null) {
                for (MFeeUser feeUser : feeUserList1) {
                    if (StringUtils.isBlank(feeUser.getOneDept())) {//取出来部门名称为空，则显示---
                        feeUser.setOneDept("---");
                    }
                    if (StringUtils.isBlank(feeUser.getTwoDept())) {
                        feeUser.setTwoDept("---");
                    }
                }
            }
            tidings.setT(feeUserList1).setPage(page);
            tidings.setStatus(Const.SUCCESS);
        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("数据异常");
            e.printStackTrace();
        }
        return tidings;
    }

    /*
    *发起缴费
    */

    @RequestMapping("/merchant/HandPayment/insetPayMent")
    @ResponseBody
    public Object getProjectType(Page page){
        Tidings<Object> tidings = new Tidings<>();
        String corpId = this.getUser().getCorpId();
        PageData pd = this.getPageData();
        String projectTypeId= pd.getString("projectTypeId");//项目类型id
        String totalAmt= pd.getString("totalAmt");//应缴费金额
        String feeUserIds= pd.getString("feeUserId");//缴费用户编号
        String projectId= pd.getString("projectId");//项目编号
        try {
            if(feeUserIds!=null){
                String[] feeUserIdArr = feeUserIds.split(",");
                for (String feeUserId : feeUserIdArr) {
                    if (feeUserId!=null){
                        Payment payment=new Payment();
                        payment.setPayId(Long.parseLong(UuidUtil.getNumFormatUUID()));//
                        payment.setFeeUserId(feeUserId);
                        payment.setTotalAmt(Double.parseDouble(totalAmt));
                        payment.setStatus("1");
                        payment.setPayStatus("04");
                        payment.setCreator(this.getUser().getNickname());
                        payment.setCreateDate(new Date());
                        paymentService.addPayment(payment);//支付主表插入数据
                        MPaymentDetail mapymentDetail=new MPaymentDetail();
                        mapymentDetail.setPayId(payment.getPayId());
                        mapymentDetail.setProjectId(Long.parseLong(projectId));
                        mapymentDetail.setTotalAmt(Double.parseDouble(totalAmt));
                        paymentDetialService.addPaymentDetail(mapymentDetail);//支付详情插入数据
                    }
                }
            }

            page.setPd(pd);
            tidings.setStatus(Const.SUCCESS);
            tidings.setMsg("生成费用成功，请在收费查询模块查看!");

        } catch (Exception e) {
            tidings.setStatus(Const.ERROR);
            tidings.setMsg("生成费用失败!");
            e.printStackTrace();
        }
        return tidings;
    }



}
